import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class UiUpdater implements Runnable
{
    DefaultListModel listModel = ChatClientUI.listModel;

    Map<String, String> parameterMap;

    Map<String, ChatWindow> userChatWindow = ChatClientUI.userChatWindow;

    public UiUpdater(Map<String, String> parameterMap)
    {
        this.parameterMap = parameterMap;
    }

    public void run()
    {

        handleServerMessages();
    }

    private void handleLoginResponse(String usersCsv)
    {
        System.out.println("handleLoginResponse*********BEFORE******************="+ChatClientUI.isLoggedIn);
        ChatClientUI.isLoggedIn = true;
        System.out.println("handleLoginResponse*********AFTER******************="+ChatClientUI.isLoggedIn);
        List<String> loggedInUsers = new ArrayList<String>(Arrays.asList(usersCsv.split(",")));
        for (String user : loggedInUsers)
        {
            listModel.addElement(user);
        }
        
    }

    private void handleLogoutResponse(String username)
    {
        listModel.removeElement(username);
    }

    private void handleMessage(String from, String msg)
    {
        ChatWindow chatWindow = userChatWindow.get(from);
        if (chatWindow == null)
        {
            try
            {
                chatWindow = new ChatWindow(from);
                userChatWindow.put(from, chatWindow);
            }
            catch (Exception e)
            {
                System.out.println("Could not open ChatWindow : " + e);
            }
        }
        chatWindow.setMessages(" " + from + " : " + msg);
    }

    private void handleServerMessages()
    {
        if (parameterMap.get("event") != null)
        {
            if (parameterMap.get("event").equals(ResponseCodes.LOGIN_RESPONSE_CODE))
            {
                handleLoginResponse(parameterMap.get("msg"));
            }
            if (parameterMap.get("event").equals(ResponseCodes.LOGOUT_RESPONSE_CODE))
            {
                handleLogoutResponse(parameterMap.get("msg"));
            }
            if (parameterMap.get("event").equals(ResponseCodes.SEND_RESPONSE_CODE))
            {
                handleMessage(parameterMap.get("from"), parameterMap.get("msg"));
            }
            if (parameterMap.get("event").equals(ResponseCodes.FIRST_LOGIN_RESPONSE_CODE))
            {
                System.out.println("FIRST_LOGIN_RESPONSE_CODE*********BEFORE******************="+ChatClientUI.isLoggedIn);
                ChatClientUI.isLoggedIn = true;
                System.out.println("FIRST_LOGIN_RESPONSE_CODE*********AFTER******************="+ChatClientUI.isLoggedIn);
                showMessageDialog("Welcome to chat app.You are the first to login !!!",
                        "First Login");
                
            }
            if (parameterMap.get("event").equals(ResponseCodes.ALREADY_LOGGED_RESPONSE_CODE))
            {
                showMessageDialog(
                        "Already an user logged in with the same name.Please try different name",
                        "Message Box");
            }
            if (parameterMap.get("event").equals(ResponseCodes.CHANNEL_IN_USE_RESPONSE_CODE))
            {
                showMessageDialog("Already an user logged in with this channel", "Message Box");
            }
        }
    }

    private void showMessageDialog(String msg, String title)
    {
        JOptionPane.showMessageDialog(null, msg, title, JOptionPane.OK_OPTION);
    }

}